package com.hyst.web;



import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import org.dom4j.Document;   
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;   
import org.dom4j.Element; 

@RestController
@RequestMapping("SMSverification")
@CrossOrigin(maxAge = 3600, origins = "*")
public class SMSverificationComtroller {
	static Map<String, String> msgMap = new HashMap<String, String>();
	@GetMapping("phone")
	public String verification( final String phone) {
		String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit";
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);
		client.getParams().setContentCharset("GBK");
		method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK");
		int mobile_code = (int)((Math.random()*9+1)*100000);
	    String content = new String("您的验证码是："+mobile_code+"。请不要把验证码泄露给其他人。");	    
		NameValuePair[] data = {//提交短信
			    new NameValuePair("account", "C04600536"), 
			    new NameValuePair("password", "01d357f88e802a8ba59e73dd90ad6097"), 
			    new NameValuePair("mobile", phone), 
			    new NameValuePair("content", content),
		};
		method.setRequestBody(data);
		try {
			client.executeMethod(method);
			String SubmitResult =method.getResponseBodyAsString();
			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();
			String code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");
			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);
			 if("2".equals(code)){
				System.out.println("短信提交成功");
				msgMap.put(phone, mobile_code+"");
			}
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return mobile_code+"";
	}
	@GetMapping("verification")
	public int rightOrWrong( final String phones) {
		int flag = 0;
		System.out.println("验证被调用:"+phones);
		String[] phone = phones.split("\\$");
		System.out.println(phone[0]);
		System.out.println(phone[1]);
		if(phone.length!=2){
			flag = 1;
		}else if(msgMap.get(phone[0])!=null&&msgMap.get(phone[0]).equals(phone[1])){
			flag = 2;
			msgMap.remove(phone[0]);			
		}		
		return flag;
	}
}
